/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.search;

import com.google.inject.Inject;
import com.google.inject.Provider;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;

import java.io.IOException;

public class LuceneServices {
  static final String INDEX_DIRECTORY = "search-directory";
  static final String INDEX_WRITER = "search-index-writer";
  static final String INDEX_READER = "search-index-reader";
  static final String INDEX_SEARCHER = "search-index-searcher";
  static final String INDEX_ANALYZER = "search-analyzer";
  private Provider<IndexReader> readerProvider;
  private Provider<IndexWriter> writerProvider;
  private Provider<IndexSearcher> searcherProvider;
  private Directory directory;
  private Analyzer analyzer;

  @Inject
  public LuceneServices(Provider<IndexReader> readerProvider, Provider<IndexWriter> writerProvider,
                        Provider<IndexSearcher> searcherProvider, Directory directory, Analyzer analyzer) {
    this.readerProvider = readerProvider;
    this.writerProvider = writerProvider;
    this.searcherProvider = searcherProvider;
    this.directory = directory;
    this.analyzer = analyzer;
  }

  public void withReader(IndexReadHandler indexReadHandler) throws IOException {
    IndexReader reader = readerProvider.get();
    try {
      indexReadHandler.handleReader(reader);
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
        LogFactory.getLog(getClass()).error("Error closing reader", e);
      }
    }
  }

  public void withWriter(IndexWriteHandler indexWriteHandler) throws IOException {
    IndexWriter writer = writerProvider.get();
    try {
      indexWriteHandler.handleWriter(writer);
    } finally {
      try {
        writer.close();
      } catch (IOException e) {
        LogFactory.getLog(getClass()).error("Error closing writer", e);
      }
    }
  }

  public void newIndex() throws IOException {
    IndexReader.unlock(directory);
    IndexWriter w = new IndexWriter(directory, analyzer, true);
    w.close();
  }

  public void withSearcher(IndexSearchHandler indexSearchHandler) {
    IndexSearcher searcher = searcherProvider.get();
    try {
      indexSearchHandler.handleSearcher(searcher, analyzer);
    } catch (Exception e) {
      LogFactory.getLog(getClass()).error("Error searching", e);
    } finally {
      try {
        searcher.close();
      } catch (IOException e) {
        LogFactory.getLog(getClass()).error("Error closing searcher", e);
      }
    }
  }

  public void optimize() throws IOException {
    withWriter(new IndexWriteHandler() {
      public void handleWriter(IndexWriter writer) throws IOException {
        LogFactory.getLog(getClass()).info("Optimizing Search Index");
        writer.optimize();
      }
    });
  }
}
